package com.hyjx.framework.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;

/**
 * 获取springbean对象工具类
 *
 * @author Administrator
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {
    private static ServletContext sc = null;
    private static BeanFactory beanFactory;
    private static WebApplicationContext wctx;
    private static WebApplicationContext wac;
    private static ApplicationContext applicationContext;
    /**
     * 日志工具
     */
    private static Logger logger = LoggerFactory.getLogger(SpringBeanUtil.class);
    
    static {
        wac = ContextLoader.getCurrentWebApplicationContext();
    }
    
    public static void setServletContext(ServletContext sc) {
        SpringBeanUtil.sc = sc;
        wctx = (WebApplicationContext) sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
    }
    
    /**
     * 取spring管理的bean
     *
     * @param beanName
     * @return spring托管的单例对象
     */
    public static Object getBean(String beanName) {
        if (wac != null && wac.containsBean(beanName)) {
            return wac.getBean(beanName);
        }
        if (wctx != null && wctx.containsBean(beanName)) {
            return wctx.getBean(beanName);
        }
        if (applicationContext != null && applicationContext.containsBean(beanName)) {
            return applicationContext.getBean(beanName);
        }
        return beanFactory.getBean(beanName);
    }
    
    /**
     * 取指定类型的类实例
     *
     * @param beanName
     * @param clazs
     * @return
     */
    public static <T> T getBean(String beanName, Class<T> clazs) {
        return clazs.cast(getBean(beanName));
    }
    
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        SpringBeanUtil.beanFactory = beanFactory;
    }
    
    /**
     * 获得所有bean对象的名称，目前仅测试用
     */
    @SuppressWarnings("unused")
    public static void showBeanNames() {
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        ServletContext servletContext = wac.getServletContext();
        try {
            WebApplicationContext wctx = (WebApplicationContext) sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");
            String[] beans = wctx.getBeanDefinitionNames();
            for (String bean : beans) {
                logger.warn(bean);
            }
        } catch (Exception e) {
        }
    }
    
    /**
     * 获得bean工厂
     *
     * @return
     */
    public static BeanFactory getBeanFactory() {
        return SpringBeanUtil.beanFactory;
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringBeanUtil.applicationContext = applicationContext;
    }
}